package com.one.rope.mvp.web.common.dto.base;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Weijian.Liu
 * @desc 基础分页返回对象
 * @date 2020/2/20 10:53
 */
@ApiModel
@JsonInclude(JsonInclude.Include.NON_NULL)
public class BasePage<T> {

  private final int DEFAULT_PAGE_SIZE = 20;

  protected int defaultMaxPageSize() {
    return 2000;
  }

  @ApiModelProperty(value = "页码，默认是第一页")
  @JsonProperty(value = "page_no")
  private int pageNo = 1;

  @ApiModelProperty(value = "每页显示的记录数，默认是20")
  @JsonProperty(value = "page_size")
  private int pageSize = DEFAULT_PAGE_SIZE;

  @ApiModelProperty(value = "总记录数")
  @JsonProperty(value = "total_record")
  private int totalRecord;

  @ApiModelProperty(value = "总页数")
  @JsonProperty(value = "total_page")
  private int totalPage;

  @ApiModelProperty(value = "当前页记录")
  private List<T> results;

  public List<T> getResults() {
    if (results == null) {
      results = new ArrayList<>();
    }
    return results;
  }

  public void setResults(List<T> results) {
    this.results = results;
  }

  public int getPageNo() {
    if (pageNo < 1) {
      return 1;
    }
    return pageNo;
  }

  public void setPageNo(Integer pageNo) {
    this.pageNo = pageNo < 1 ? 1 : pageNo;
  }

  public int getPageSize() {
    if (pageSize < 1) {
      return DEFAULT_PAGE_SIZE;
    }
    if (pageSize > defaultMaxPageSize()) {
      return defaultMaxPageSize();
    }
    return pageSize;
  }

  public void setPageSize(Integer pageSize) {
    this.pageSize = pageSize < 1 ? DEFAULT_PAGE_SIZE : pageSize;
  }

  public int getTotalRecord() {
    return totalRecord;
  }

  public void setTotalRecord(int totalRecord) {
    this.totalRecord = totalRecord;
    /**
     * 在设置总页数的时候计算出对应的总页数，在下面的三目运算中加法拥有更高的优先级，所以最后可以不加括号。
     */
    int totalPage =
        totalRecord % pageSize == 0 ? totalRecord / pageSize : totalRecord / pageSize + 1;
    this.setTotalPage(totalPage);
  }

  public int getTotalPage() {
    return totalPage;
  }

  public void setTotalPage(int totalPage) {
    this.totalPage = totalPage;
  }

  /**
   * 是否最后一页
   */
  @JsonProperty("is_last")
  public int isLast() {
    return getPageNo() >= getTotalPage() ? 1 : 0;
  }

  /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#clone()
   */
  @Override
  public Object clone() throws CloneNotSupportedException {
    // TODO Auto-generated method stub
    return super.clone();
  }

  @JsonIgnore
  @SuppressWarnings("unchecked")
  public Class<T> getGenericTypeClass() {
    try {
      String className = ((ParameterizedType) getClass().getGenericSuperclass())
          .getActualTypeArguments()[0]
          .getTypeName();
      Class<?> clazz = Class.forName(className);
      return (Class<T>) clazz;
    } catch (Exception e) {
      throw new IllegalStateException(
          "Class is not parametrized with generic type!!! Please use extends <> ");
    }
  }

  public void initPageInfo(IPage page) {
    setPageNo((int) page.getCurrent());
    setPageSize((int) page.getSize());
    setTotalPage((int) page.getPages());
    setTotalRecord((int) page.getTotal());
  }

}